export const toFixed=(num:number, digit = 3)=>{
    const scale = 10 ** digit
    return Math.floor(num * scale)/scale
}
/**
 * 字符串编码函数
 * 这个函数的作用是将一个字符串编码成 UTF-8 格式的字节数组（Uint8Array）。
 * 遍历字符串的每一个字符
 * 通过charCodeAt得到每个字符的unicode值
 * 判断unicode值处于哪个之间返回出对应数据结构
 * 返回Uint8Array数组
 * @param str 
 * @returns Uint8Array
 */
export const strencode = (str: string) => {
let byteArray: number[] = [];
for (let i = 0; i < str.length; i++) {
    let charCode = str.charCodeAt(i);
    if (charCode <= 0x7f) {
    byteArray.push(charCode);
    } else if (charCode <= 0x7ff) {
    byteArray.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f));
    } else if (charCode <= 0xffff) {
    byteArray.push(0xe0 | (charCode >> 12), 0x80 | ((charCode & 0xfc0) >> 6), 0x80 | (charCode & 0x3f));
    } else {
    byteArray.push(
        0xf0 | (charCode >> 18),
        0x80 | ((charCode & 0x3f000) >> 12),
        0x80 | ((charCode & 0xfc0) >> 6),
        0x80 | (charCode & 0x3f)
    );
    }
}
return new Uint8Array(byteArray);
};
  
/**
 * 字符串解码函数
 * 这个函数的作用是将 UTF-8 格式的字节数组解码还原成字符串。
 * @param bytes 
 * @returns 
 */
export const strdecode = (bytes: Uint8Array) => {
let array: number[] = [];
let offset = 0;
let charCode = 0;
let end = bytes.length;
while (offset < end) {
    if (bytes[offset] < 128) {
    charCode = bytes[offset];
    offset += 1;
    } else if (bytes[offset] < 224) {
    charCode = ((bytes[offset] & 0x3f) << 6) + (bytes[offset + 1] & 0x3f);
    offset += 2;
    } else if (bytes[offset] < 240) {
    charCode = ((bytes[offset] & 0x0f) << 12) + ((bytes[offset + 1] & 0x3f) << 6) + (bytes[offset + 2] & 0x3f);
    offset += 3;
    } else {
    charCode =
        ((bytes[offset] & 0x07) << 18) +
        ((bytes[offset + 1] & 0x3f) << 12) +
        ((bytes[offset + 1] & 0x3f) << 6) +
        (bytes[offset + 2] & 0x3f);
    offset += 4;
    }
    array.push(charCode);
}
return String.fromCharCode.apply(null, array);
};
//伪随机
export const randomBySeed=(seed:number)=>{
    return seed=(seed*9301+49297) % 233280
}